﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using System.Text;
using System.Web.Services;

using CE.Database;
using CE.Mail;
using CE.Global;
using CE.CarInsurance;
using CE.User;
using CE.Security;

public partial class car_insurance_call_me : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            divCallMe.Visible = true;
            divCallMeMessage.Visible = false;
            if (Request.QueryString["sent"] != null && Request.QueryString["sent"].ToString() != string.Empty && Request.QueryString["sent"].ToString() == "1")
            {
                divCallMeMessage.Visible = true;
                divCallMe.Visible = false;
            }
            LoadTitles();
            txtUserName.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtUserName.ClientID + "','" + rfvUserName.ClientID + "','" + revUserName.ClientID + "');DefaultName(this, event);");
            txtUserName.Attributes.Add("onfocus", "ClearText(this, event);");
            txtUserEmail.Attributes.Add("onblur", "RemoveSpace(this);ShowValidationEffectTwo('" + txtUserEmail.ClientID + "','" + rfvUserEmail.ClientID + "','" + revUserEmail.ClientID + "');DefaultEmail(this, event);");
            txtUserEmail.Attributes.Add("onfocus", "ClearText(this, event);");
            txtUserMobile.Attributes.Add("onblur", "RemoveSpace(this);ShowValidationEffectTwo('" + txtUserMobile.ClientID + "','" + rfvUserMobile.ClientID + "','" + revUserMobile.ClientID + "');DefaultPhone(this, event);");
            txtUserMobile.Attributes.Add("onfocus", "ClearText(this, event);");

            HttpCookie _cookie = Request.Cookies.Get("GGMobileQuote");
            if (_cookie != null)
            {
                if (_cookie != null)
                {
                    txtUserEmail.Text = MySecurity.Decrypt(_cookie.Values.Get("CustomerEmail"), true);
                    txtUserName.Text = MySecurity.Decrypt(_cookie.Values.Get("CustomerName"), true);
                    txtUserMobile.Text = MySecurity.Decrypt(_cookie.Values.Get("CustomerMobile"), true);
                }
            }
            if (Request.QueryString["qid"] != null && Request.QueryString["qid"].ToString() != string.Empty)
            {
                hdnQuoteId.Value = Request.QueryString["qid"].ToString();
            }
            if (Request.QueryString["cid"] != null && Request.QueryString["cid"].ToString() != string.Empty)
            {
                hdnCarId.Value = Request.QueryString["cid"].ToString();
            }
            if (Request.QueryString["pid"] != null && Request.QueryString["pid"].ToString() != string.Empty)
            {
                hdnPolicyId.Value = Request.QueryString["pid"].ToString();
            }
        }
    }

    protected void ibtnBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("results.aspx?type=email&qid=" + hdnQuoteId.Value + "&cid=" + hdnCarId.Value + "&pid=" + hdnPolicyId.Value);
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            ChangeValidationEffectServerSide("callme");
            Page.Validate("callme");
            if (Page.IsValid)
            {
                Int64 _userId = 0;
                Int64 _carId = 0;
                Int64 _carResultId = 0;
                Int64 _ProviderId = 0;

                if (Session[General.UserId] != null)
                {
                    if (!Int64.TryParse(Session[General.UserId].ToString(), out _userId))
                    {
                        _userId = 0;
                    }
                }
                if (Session[General.CarId] != null)
                {
                    if (!Int64.TryParse(Session[General.CarId].ToString(), out _carId))
                    {
                        _carId = 0;
                    }
                }

                if (Request.QueryString["prid"] != null && Request.QueryString["prid"].ToString() != string.Empty)
                {
                    if (!Int64.TryParse(Request.QueryString["prid"].ToString(), out _ProviderId))
                    {
                        _ProviderId = 0;
                    }
                }

                if (Request.QueryString["rid"] != null && Request.QueryString["rid"].ToString() != string.Empty)
                {
                    if (!Int64.TryParse(Request.QueryString["rid"].ToString(), out _carResultId))
                    {
                        _carResultId = 0;
                    }
                }

                if (_userId > 0 && _carId > 0 && _ProviderId > 0)
                {
                    string _strCallMeTime = ddlCallMeTime.Text;
                    string strEmail = Server.HtmlEncode(txtUserEmail.Text.Trim());
                    string strName = Server.HtmlEncode(txtUserName.Text.Trim());
                    string strMobile = Server.HtmlEncode(txtUserMobile.Text.Trim());

                    #region Call Me
                    string _message = "";
                    string _title = "";

                    _message = "Customer Name: " + Server.HtmlEncode(txtUserName.Text);
                    _title = "Call Me Now - Call Me request by " + Server.HtmlEncode(txtUserName.Text);

                    Int64 _result = 0;
                    ContactUser _cu = new ContactUser();
                    ContactUserDB _cudb = new ContactUserDB();
                    string strInsType = "";
                    if(Request.Params["instype"] != null && Request.Params["instype"].ToString() != string.Empty)
                    {
                        strInsType = Request.Params["instype"].ToString();
                    }
                    if (strInsType == "10")
                    {
                        strInsType = "1";
                    }
                    else if (strInsType == "11")
                    {
                        strInsType = "1+";
                    }
                    else if (strInsType == "21")
                    {
                        strInsType = "2+";
                    }
                    else if (strInsType == "30")
                    {
                        strInsType = "3+";
                    }
                    else if (strInsType == "31")
                    {
                        strInsType = "3+";
                    }

                    _cu.InsuranceType = "CI-" + strInsType;
                    _cu.ProductId = _carId;
                    _cu.RefCEUserId = _userId;
                    _cu.ProviderCompanyId = _ProviderId;
                    _cu.ContactNo = Server.HtmlEncode(txtUserMobile.Text);
                    _cu.FullName = Server.HtmlEncode(txtUserName.Text);
                    _cu.CallTime = Convert.ToInt16(ddlCallMeTime.Text);

                    if (Request.Params["spc"] != null)
                    {
                        if (Request.Params["spc"].ToString() == "1")
                        {
                            _cu.Info1 = "GG Promotion";
                        }
                    }

                    _result = _cudb.Insert(_cu);

                    if (_result > 0)
                    {
                        _message += "<img src='http://www.gluaygluay.com/images/callme/callme-home.png' /><br/>";
                        _message += "<br/><br/>Insurance Type : " + strInsType;
                        _message += "<br/><br/>Full Name : " + Server.HtmlEncode(txtUserName.Text);
                        _message += "<br/><br/>Email : " + txtUserEmail.Text;
                        _message += "<br/><br/>Contact No : " + Server.HtmlEncode(txtUserMobile.Text);
                        _message += "<br/><br/>Call Time : " + ddlCallMeTime.SelectedItem.Text;
                        string _emailList = ConfigurationManager.AppSettings["Mobile.TH.CICallMeReceiver"].ToString();
                        MyMailManager.SendNotification(_emailList, _title, _message);

                    }

                    #endregion
                    Response.Redirect("call-me.aspx?rid=" + _carResultId.ToString() + "&prid=" + _ProviderId.ToString() + "&qid=" + hdnQuoteId.Value + "&cid=" + hdnCarId.Value + "&pid=" + hdnPolicyId.Value + "&sent=1");
                }
            }
        }
        catch (Exception ex)
        {
        }
    }

    private void LoadTitles()
    {
        lblApplicationTitle.Text = Resources.CarMenu.CallYouBack;
        lblCallMeTimeTitle.Text = Resources.CarMenu.CallMeAt;
        lblThanksTitle.Text = Resources.CarMenu.ThankYou;
        lblMessage1.Text = Resources.CarMenu.CallMeMessage;
        lblMessage2.Text = Resources.CarMenu.HotLineMessage;
        btnSubmit.Text = Resources.ProposalForm.Submit;
        ibtnBack.ImageUrl = Resources.CarMenu.ButtonBackUrl;
    }

    private void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        txtUserName.Text = HttpUtility.HtmlEncode(txtUserName.Text.Trim());
        txtUserEmail.Text = HttpUtility.HtmlEncode(txtUserEmail.Text);
        txtUserMobile.Text = HttpUtility.HtmlEncode(txtUserMobile.Text);

        #region DOB
        //Day
        if (rfvCallMeTime.Enabled && rfvCallMeTime.ValidationGroup == strValidationGroup)
        {
            rfvCallMeTime.Validate();
            if (rfvCallMeTime.IsValid == false)
            {
                ddlCallMeTime.ControlStyle.CssClass = "your-detail-fields validationError";
            }
            else
            {
                ddlCallMeTime.ControlStyle.CssClass = "your-detail-fields-2";
            }
        }
        else
        {
            ddlCallMeTime.ControlStyle.CssClass = "your-detail-fields-2";
        }
        #endregion

        #region user
        //Name
        if (rfvUserName.Enabled && rfvUserName.ValidationGroup == strValidationGroup)
        {
            rfvUserName.Validate();
            if (rfvUserName.IsValid == false)
            {
                txtUserName.ControlStyle.CssClass = "your-detail-fields validationError";
            }
            else
            {
                txtUserName.ControlStyle.CssClass = "your-detail-fields-1";
            }
        }
        else
        {
            txtUserName.ControlStyle.CssClass = "your-detail-fields-1";
        }

        if (revUserName.Enabled && revUserName.ValidationGroup == strValidationGroup)
        {
            if (rfvUserName.IsValid)
            {
                revUserName.Validate();
                if (revUserName.IsValid == false)
                {
                    txtUserName.ControlStyle.CssClass = "your-detail-fields validationError";
                }
                else
                {
                    txtUserName.ControlStyle.CssClass = "your-detail-fields-1";
                }
            }
        }
        else
        {
            txtUserName.ControlStyle.CssClass = "your-detail-fields-1";
        }

        //Email
        if (rfvUserEmail.Enabled && rfvUserEmail.ValidationGroup == strValidationGroup)
        {
            rfvUserEmail.Validate();
            if (rfvUserEmail.IsValid == false)
            {
                txtUserEmail.ControlStyle.CssClass = "your-detail-fields validationError";
            }
            else
            {
                txtUserEmail.ControlStyle.CssClass = "your-detail-fields-1";
            }
        }
        else
        {
            txtUserEmail.ControlStyle.CssClass = "your-detail-fields-1";
        }

        if (revUserEmail.Enabled && revUserEmail.ValidationGroup == strValidationGroup)
        {
            if (rfvUserEmail.IsValid)
            {
                revUserEmail.Validate();
                if (revUserEmail.IsValid == false)
                {
                    txtUserEmail.ControlStyle.CssClass = "your-detail-fields validationError";
                }
                else
                {
                    txtUserEmail.ControlStyle.CssClass = "your-detail-fields-1";
                }
            }
        }
        else
        {
            txtUserEmail.ControlStyle.CssClass = "your-detail-fields-1";
        }

        //Mobile No
        if (rfvUserMobile.Enabled && rfvUserMobile.ValidationGroup == strValidationGroup)
        {
            rfvUserMobile.Validate();
            if (rfvUserMobile.IsValid == false)
            {
                txtUserMobile.ControlStyle.CssClass = "your-detail-fields validationError";
            }
            else
            {
                txtUserMobile.ControlStyle.CssClass = "your-detail-fields-1";
            }
        }
        else
        {
            txtUserMobile.ControlStyle.CssClass = "your-detail-fields-1";
        }

        if (revUserMobile.Enabled && revUserMobile.ValidationGroup == strValidationGroup)
        {
            if (rfvUserMobile.IsValid)
            {
                revUserMobile.Validate();
                if (revUserMobile.IsValid == false)
                {
                    txtUserMobile.ControlStyle.CssClass = "your-detail-fields validationError";
                }
                else
                {
                    txtUserMobile.ControlStyle.CssClass = "your-detail-fields-1";
                }
            }
        }
        else
        {
            txtUserMobile.ControlStyle.CssClass = "your-detail-fields-1";
        }
        #endregion
    }


}
